package com.qxzn.app.desktoprobot.receive;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.Constant;

import cn.jpush.android.api.JPushInterface;

public class PushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        AppUtil.showLog("PushReceiver.onReceive");
        try {
            Bundle bundle = intent.getExtras();
            //Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
            //mContext = context;
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                AppUtil.showLog("[MyReceiver] 接收Registration Id : " + regId);
                //send the Registration Id to your server...
                // Toast.makeText(context,regId,Toast.LENGTH_LONG).show();

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                String dataStr = bundle.getString(JPushInterface.EXTRA_MESSAGE);
                AppUtil.showLog("[MyReceiver] 接收到推送下来的自定义消息: " + dataStr);
                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(dataStr);
                int code = jsonObject.getIntValue("code");
                switch (code) {
                    case 0: //语音通话
                        break;
                    case 1: //视频通话
                        if (!"com.qxzn.module.agora.VideoChatCallActivity".equals(ActivityStack.currentActivityName())) {
                            com.alibaba.fastjson.JSONObject dataObject = jsonObject.getJSONObject("data");
                            context.startActivity(new Intent().setComponent(new ComponentName(context, "com.qxzn.module.agora.VideoChatReceiveActivity"))
                                    .putExtra("headPortrait", dataObject.getString("headportrait")).putExtra("nickname", dataObject.getString("nickname"))
                                    .putExtra("sender", dataObject.getString("sender")));
                        }
                        break;
                    case 2: //视频监控
                        break;
                    case 3: //聊天消息
                        AppUtil.showLog("聊天消息");
                        break;
                    case 5: //视频聊天挂断
                        AppUtil.showLog("视频聊天挂断");
                        com.alibaba.fastjson.JSONObject dataObj = jsonObject.getJSONObject("data");
                        if (dataObj != null) {
                            EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.HUNG_UP_VIDEO)
                                    .intMsg(dataObj.getIntValue("refusetype")).msg(dataObj.getString("username")).build());
                        }
                        break;
                    case 103: //收到加入家庭圈的邀请
                        com.alibaba.fastjson.JSONObject dataObject = jsonObject.getJSONObject("data");
                        EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.INVITATION_TO_MEMBER).object(dataObject).build());
                        break;
                }
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                AppUtil.showLog("[MyReceiver] 接收到推送下来的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                AppUtil.showLog("[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                AppUtil.showLog("[MyReceiver] 用户点击打开了通知");

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                AppUtil.showLog("[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                AppUtil.showLog("[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
            }

        } catch (Exception e) {

        }
    }
}
